package com.railway.switchs.utils;

import com.railway.switchs.domain.other.EnumInfo;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName EnumUtil
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Slf4j
public class EnumUtil {

    private static final String dot = ".";
    private static final List<String> packages = Arrays.asList("com.railway.switchs.enums");

    /**
     * 根据类名找枚举类
     * @param className
     * @return
     * @throws ClassNotFoundException
     */
    private static Class<?> findEnumClass(String className) throws ClassNotFoundException {
        Class enumClass = null;
        for (String p : packages) {
            try {
                enumClass = Class.forName(p + dot + className);
            } catch (ClassNotFoundException e) {
                continue;
            }
            break;
        }
        if (enumClass == null || !enumClass.isEnum()) {
            throw new ClassNotFoundException("在路径"+packages+"下没有找到类名为" + className + "的枚举类" );
        }
        return enumClass;
    }

    /**
     * 根据code取name的值
     * @param className
     * @param code
     * @return
     */
    public static String getEnumValue(String className, String code) {
        try {
            Class<?> enumClass = findEnumClass(className);

            Method listValues = enumClass.getMethod("values");
            Method codeMethod = enumClass.getMethod("getCode");
            Method nameMethod = enumClass.getMethod("getName");
            Object[] objs = (Object[]) listValues.invoke(null);
            String result = null;
            for (Object obj : objs) {
                Object cast = enumClass.cast(obj);
                Object invoke = codeMethod.invoke(cast);

                if (invoke != null && code.equals(invoke.toString())) {
                    Object v = nameMethod.invoke(cast);
                    if (v != null) {
                        result = v.toString();
                    }

                    break;
                }
            }

            return null == result ? "" : result;
        } catch (Exception e) {
            log.error("获取对应的枚举值失败:{}", e.getMessage());
            return null;
        }
    }

    /**
     * 根据类名获取枚举值的list
     * @param className
     * @return
     */
    public static List<EnumInfo> getEnumList(String className) {
        List<EnumInfo> result = new ArrayList<EnumInfo>();
        try {
            Class<?> enumClass = findEnumClass(className);

            Method listValues = enumClass.getMethod("values");
            Method codeMethod = enumClass.getMethod("getCode");
            Method nameMethod = enumClass.getMethod("getName");
            Object[] objs = (Object[]) listValues.invoke(null);

            for (Object obj : objs) {
                Object cast = enumClass.cast(obj);
                Object invoke = codeMethod.invoke(cast);

                if (invoke != null) {
                    Object v = nameMethod.invoke(cast);
                    if (v != null) {
                        EnumInfo rn = new EnumInfo();
                        rn.setCode(invoke.toString());
                        rn.setName(v.toString());
                        result.add(rn);
                    }
                }
            }
        } catch (Exception e) {
            log.error("获取枚举值失败:{}", e.getMessage());
            return null;
        }
        return result;
    }

}
